home *** CD-ROM | disk | FTP | other *** search
/ Interplay's Learn to Program Basic (Review Copy) / Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO / pc / ltpbasic / projects / blockhed.bas next >
Encoding:
BASIC Source File  |  1998-03-03  |  7.8 KB  |  479 lines

  1. Rem BlockHead
  2. Rem by Andrew Howat
  3. Rem Use the arrow keys to 
  4. Rem control the "Head" and 
  5. Rem try to clear as many 
  6. Rem bricks as possible
  7.  
  8. Rem See the bottom of
  9. Rem this program for some
  10. Rem ideas for changes.
  11.  
  12. paddleWidth = 40
  13. paddleSpeed = 2
  14.  
  15. blockRows = 2
  16. blockCols = 8
  17.  
  18. blockWidth = 40
  19. blockHeight = 24
  20.  
  21. ballSize = 6
  22. ballHalfSize = 3
  23.  
  24. ballExtraSpeed = 0
  25.  
  26. numLives = 3
  27. score = 0
  28.  
  29. minTime = 15
  30.  
  31. Dim blocks(blockCols,blockRows)
  32.  
  33. rem -------------------
  34. rem Program Start
  35. rem -------------------
  36. Cls
  37.  
  38. Background  "BrkOut" 
  39.  
  40. bounceSnd = LoadSound("BOUNCE")
  41. headHitSnd = LoadSound("HEADCONK")
  42. fallThruSnd = LoadSound("GAMESHOW")
  43. row1HitSnd = LoadSound("BONKUP")
  44. row2HitSnd = LoadSound("BONK")
  45. startLevelSound$ = "VICTORY"
  46. gameOverSound$ = "GAMOVER2"
  47. clearedLevelSound$ = "CROWD"
  48.  
  49. screenWidth = 320
  50. screenHeight = 240
  51.  
  52. Gosub SetupBlocks
  53.  
  54. Gosub SetupPaddle
  55.  
  56. Gosub SetupBall
  57.  
  58. Rem Fill bottom of screen to be used for lives and score.
  59. Color 3 'Black
  60. FillRect 0,222 To 320,240
  61.  
  62. Gosub ShowLivesAndScore
  63.  
  64. lastTime = Timer()
  65. RunGame = TRUE
  66. While RunGame = TRUE
  67.  
  68. Gosub ProcessInput
  69.  
  70. Gosub MoveBall
  71.  
  72. Gosub ShowPaddleAndBall
  73.  
  74. Rem Enforce a limiting speed for fast machines
  75. while timer() < lastTime + minTime
  76. wend
  77. lastTime = timer()
  78.  
  79. Wend
  80.  
  81. Color 3 'Black
  82. FillRect 100,75 To 220,120
  83. TextColor 6
  84. Position 16, 5
  85. Print "Game Over"
  86. Position 15, 6
  87. Print "Score = " score
  88.  
  89. Rem Then play Game Over sound
  90. Sound(gameOverSound$)
  91. Rem Delay to let sound finish!
  92. Sleep 30
  93.  
  94. End
  95.  
  96. rem -------------------
  97. SetupBlocks:
  98. rem -------------------
  99.  
  100. tmpFrame = 0
  101. blockCount = 0
  102.  
  103. for row = 1 to blockRows
  104. for col = 1 to blockCols
  105.  
  106. blocks(col,row) = LoadSprite("Blocks")
  107.  
  108. If row = 1 Then
  109. Rem Pick one of the eyes
  110. tmpFrame = random(2,4)
  111. Else
  112. If row = 2 Then
  113. Rem Pick one of the noses
  114. tmpFrame = random(0,1)
  115. Else
  116. Rem Pick one of the mouths
  117. tmpFrame = random(5,6)
  118. EndIf
  119. EndIf
  120.  
  121. SetSprite blocks(col,row) To (col-1)*blockWidth, (row-1)*blockHeight
  122. SetSprite blocks(col,row) Frame tmpFrame
  123.  
  124. Rem Keep track of how many blocks there are.
  125. blockCount = blockCount + 1
  126.  
  127. Next col
  128. Next row
  129.  
  130. Rem Play the sound that we're ready
  131. Rem to start a new level!
  132. Sound(startLevelSound$)
  133.  
  134. Return
  135.  
  136. rem -------------------
  137. SetupPaddle:
  138. rem -------------------
  139. paddle = LoadSprite("BrkOut")
  140.  
  141. Rem Set starting position of paddle
  142. paddleX = 100
  143. paddleY = 190
  144. SetSprite paddle To paddleX,paddleY
  145. SetSprite paddle Frame 2
  146.  
  147. hitPaddle = 0
  148.  
  149. Return
  150.  
  151. rem -------------------
  152. SetupBall:
  153. rem -------------------
  154. ball = LoadSprite("BrkOut")
  155.  
  156. Gosub RestartBall
  157.  
  158. Return
  159.  
  160. rem -------------------
  161. RestartBall:
  162. rem -------------------
  163.  
  164. Rem Position ball in middle of paddle
  165. ballX = paddleX + (paddleWidth/2)
  166. ballY = paddleY
  167.  
  168. Rem Set the initial speed of the ball
  169. ballSpeedX = 1
  170. ballSpeedY = -1
  171.  
  172. SetSprite ball To ballX,ballY
  173. SetSprite ball Frame 0
  174.  
  175. Rem Pause a moment to let player
  176. Rem get ready
  177. HideSprite ball
  178. Sleep 10
  179. ShowSprite ball
  180.  
  181. Return
  182.  
  183. rem -------------------
  184. ShowPaddleAndBall:
  185. rem -------------------
  186.  
  187. If hitPaddle = 0 Then
  188. SetSprite paddle Frame 2
  189. Else
  190. hitPaddle = hitPaddle - 1
  191. EndIf
  192.  
  193. SetSprite paddle To paddleX,paddleY
  194.  
  195. SetSprite ball To ballX-ballHalfSize, ballY-ballHalfSize
  196.  
  197. Return
  198.  
  199. rem -------------------
  200. ProcessInput:
  201. rem -------------------
  202.  
  203. If KeyDown("CURRIGHT") Then
  204. paddleX = paddleX + paddleSpeed
  205.  
  206. If paddleX > (screenWidth-paddleWidth) Then
  207. paddleX = screenWidth-paddleWidth
  208. EndIf
  209.  
  210. Else
  211. If KeyDown("CURLEFT") Then
  212. paddleX = paddleX - paddleSpeed
  213.  
  214. If paddleX < 0 Then
  215. paddleX = 0
  216. EndIf
  217.  
  218. EndIf
  219. EndIf
  220.  
  221. If KeyDown("Q") THEN
  222. RunGame = 0
  223. EndIf
  224.  
  225. Return
  226.  
  227. rem -------------------
  228. MoveBall:
  229. rem -------------------
  230.  
  231. prevBallX = ballX
  232. prevBallY = ballY
  233.  
  234. ballX = ballX + ballSpeedX
  235. ballY = ballY + ballSpeedY
  236.  
  237. Rem Has the ball hit the right edge
  238. Rem of the screen?
  239. If ballX >= (screenWidth-ballHalfSize) Then
  240. PlaySound(bounceSnd)
  241. ballX = screenWidth-ballHalfSize-1
  242. ballSpeedX = -ballSpeedX
  243. Else
  244. Rem Has the ball hit the left edge?
  245. If ballX < ballHalfSize Then
  246. PlaySound(bounceSnd)
  247. ballX = ballHalfSize
  248. ballSpeedX = -ballSpeedX
  249. EndIf
  250. Endif
  251.  
  252. Rem Has the ball gone pass the top
  253. Rem of the screen?
  254. If ballY <= ballHalfSize Then
  255. PlaySound(bounceSnd)
  256. ballY = ballHalfSize
  257. ballSpeedY = -ballSpeedY
  258. Else
  259. Rem Has the ball gone pass the bottom
  260. Rem of the screen
  261. If ballY > screenHeight Then
  262. PlaySound(fallThruSnd)
  263. Rem Decrease the number of lives
  264. numLives = numLives - 1
  265.  
  266. Gosub ShowLivesAndScore
  267.  
  268. Rem Do we have any lives left
  269. If numLives > 0 Then
  270. Rem Restart ball
  271. Sleep 10
  272. Gosub RestartBall
  273. Else
  274. Rem Set flag indicating game is over.
  275. RunGame = 0
  276. EndIf
  277. EndIf
  278. EndIf
  279.  
  280. Rem Check to see if the ball has hit
  281. Rem anything.
  282. Gosub CheckCollisions
  283.  
  284. Return
  285.  
  286.  
  287. rem -------------------
  288. CheckCollisions:
  289. rem -------------------
  290.  
  291. rem Has the ball hit a block?
  292.  
  293. rem Based on the ball position, which
  294. rem block do we need to test.
  295. testCol = Int(ballX/blockWidth)+1
  296. testRow = Int(ballY/blockHeight)+1
  297.  
  298. Rem Do we need to check collisions
  299. Rem with blocks.
  300. If testRow <= blockRows Then
  301.  
  302. Rem Is there a block at the test position?
  303. If blocks(testCol,testRow) >= 0 Then
  304. Gosub HandleBlockCollision
  305. EndIf
  306.  
  307. Rem Are all the blocks gone?
  308. If blockCount = 0 Then
  309.  
  310. Sound(clearedLevelSound$)
  311.  
  312. Gosub SetupBlocks
  313.  
  314. Gosub RestartBall
  315.  
  316. Rem Speed up for next round.
  317. ballExtraSpeed = ballExtraSpeed + 1
  318. paddleSpeed = paddleSpeed + 1
  319.  
  320. EndIf
  321.  
  322. Else
  323.  
  324. Rem Has the paddle been hit?
  325.  
  326. Rem Is the ball within paddle range? 
  327. If ballY >= paddleY-ballHalfSize Then
  328. rem If  ballY <= paddleY+ballHalfSize Then
  329.  
  330. Rem Is the ball between the paddle limits
  331. If ballX >= paddleX Then
  332. If ballX <= (paddleX+paddleWidth) Then
  333. Gosub HandlePaddleCollision
  334. EndIf
  335. EndIf
  336.  
  337. rem EndIf
  338. EndIf
  339.  
  340. EndIf
  341.  
  342. Return
  343.  
  344.  
  345. rem -------------------
  346. HandlePaddleCollision:
  347. rem -------------------
  348.  
  349. Rem Is the ball too far down to
  350. Rem allow it to bounce up?
  351. If  ballY >= paddleY+ballSize Then
  352. ballSpeedX = -ballSpeedX
  353. Else
  354.  
  355. Rem Which section of the paddle
  356. Rem did the ball hit?
  357. If ballX < (paddleX+10) Then
  358. ballSpeedX = -2
  359. ballSpeedY = -1
  360. Else
  361. If ballX < (paddleX+20) Then
  362. ballSpeedX = -1
  363. ballSpeedY = -2
  364. Else
  365. If ballX < (paddleX+30) Then
  366. ballSpeedX = 1
  367. ballSpeedY = -2
  368. Else
  369. ballSpeedX = 2
  370. ballSpeedY = -1
  371. EndIf
  372. EndIf
  373. EndIf
  374.  
  375. Rem Add in any extra speed.
  376. If ballSpeedX >= 0 Then
  377. ballSpeedX = ballSpeedX + ballExtraSpeed
  378. Else
  379. ballSpeedX = ballSpeedX - ballExtraSpeed
  380. EndIf
  381.  
  382. ballSpeedY = ballSpeedY - ballExtraSpeed
  383. EndIf
  384.  
  385. Rem Set paddle face to "ouch" frame
  386. SetSprite paddle Frame 1
  387. PlaySound(headHitSnd)
  388.  
  389. Rem Set the count for how long the
  390. Rem "ouch" frame should show.
  391. hitPaddle = 30
  392.  
  393. Return
  394.  
  395. rem -------------------
  396. HandleBlockCollision:
  397. rem -------------------
  398.  
  399. Rem testRow and testCol contain the position
  400. Rem of the block that has been hit.
  401.  
  402. Rem What is the position of the block hit?
  403. blockX = (testCol-1) * blockWidth
  404. blockY = (testRow-1) * blockHeight
  405.  
  406. If prevBallX >= blockX Then 
  407. If prevBallX < (blockX + blockWidth) Then
  408. Rem Change Y direction
  409. ballSpeedY = -ballSpeedY
  410. EndIf
  411. EndIf
  412.  
  413. If prevBallY >= blockY Then
  414. If prevBallY < (blockY + blockHeight) Then
  415. Rem Change X direction
  416. ballSpeedX = -ballSpeedX
  417. EndIf
  418. EndIf
  419.  
  420. Rem Play a different sound depending
  421. Rem on which row was hit
  422. If testRow = 1 Then
  423. PlaySound(row1HitSnd)
  424. Else
  425. PlaySound(row2HitSnd)
  426. EndIf
  427.  
  428. Rem Remove the block from the screen
  429. HideSprite blocks(testCol,testRow)
  430. FreeSprite blocks(testCol,testRow)
  431.  
  432. Rem Set the block as not existing
  433. Rem in the array
  434. blocks(testCol,testRow) = -1
  435.  
  436. Rem Decrease the number of blocks left.
  437. blockCount = blockCount - 1
  438.  
  439. Rem Increase player score
  440. score = score + (blockRows-testRow)*5
  441.  
  442. Gosub ShowLivesAndScore
  443.  
  444. Return
  445.  
  446. rem -------------------
  447. ShowLivesAndScore:
  448. rem -------------------
  449.  
  450. TextColor 6 
  451. Position 1,14
  452. Print "Lives = " numLives;
  453.  
  454. Position 28,14
  455. Print "Score = " score;
  456.  
  457. Return
  458.  
  459. rem -------------------
  460.  
  461.  
  462. Rem ============================
  463. Rem SUGGESTIONS TO MAKE IT YOUR WAY!
  464. Rem 
  465. Rem - Add a High Score List. 
  466. Rem   (See the project hints for an example)
  467. Rem - Give an extra life if you
  468. Rem   complete a level
  469. Rem - Change the graphics for the
  470. Rem   blocks and/or the background
  471. Rem   with each new level.
  472. Rem - Make advanced levels that
  473. Rem   include bonus point bricks
  474.  
  475.  
  476.  
  477.  
  478.